<template>
  <el-container class="layout-container">
    <el-header class="layout-header">
      <div class="header-left">
        <h2>用户管理系统</h2>
      </div>
      <div class="header-right">
        <el-dropdown @command="handleCommand">
          <span class="user-info">
            <el-icon><User /></el-icon>
            {{ userInfo.username }}
            <el-icon class="el-icon--right"><ArrowDown /></el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </el-header>

    <el-container>
      <el-aside width="200px" class="layout-aside">
        <el-menu
            :default-active="currentRoute"
            router
            class="sidebar-menu"
        >
          <el-menu-item index="/users">
            <el-icon><User /></el-icon>
            <span>用户管理</span>
          </el-menu-item>
          <el-menu-item index="/hospitals">
            <el-icon><OfficeBuilding /></el-icon>
            <span>医院管理</span>
          </el-menu-item>
          <el-menu-item index="/logs">
            <el-icon><Document /></el-icon>
            <span>操作日志</span>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <el-main class="layout-main">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { defineComponent, ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, ArrowDown, OfficeBuilding } from '@element-plus/icons-vue'
import {userApi} from "../api/user.js";

export default defineComponent({
  name: 'Layout',
  components: {
    User,
    ArrowDown,
    OfficeBuilding
  },
  setup() {
    const router = useRouter()
    const route = useRoute()

    const userInfo = ref({
      username: '管理员'
    })

    const currentRoute = ref(route.path)

    const handleCommand = async (command) => {
      if (command === 'logout') {
        // 调后台登出接口
        await userApi.logout()
        sessionStorage.removeItem('token')
        sessionStorage.removeItem('userInfo')
        ElMessage.success('退出登录成功')
        router.push('/login')
      }
    }

    onMounted(() => {
      const savedUserInfo = sessionStorage.getItem('userInfo')
      if (savedUserInfo) {
        try {
          userInfo.value = JSON.parse(savedUserInfo)
        } catch (error) {
          console.error('解析用户信息失败:', error)
        }
      }
    })

    return {
      userInfo,
      currentRoute,
      handleCommand
    }
  }
})
</script>

<style scoped>
.layout-container {
  height: 100vh;
}

.layout-header {
  background: #409eff;
  color: white;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}

.header-left h2 {
  margin: 0;
  font-size: 20px;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  color: white;
}

.user-info .el-icon {
  margin: 0 5px;
}

.layout-aside {
  background: #f5f5f5;
  border-right: 1px solid #e6e6e6;
}

.sidebar-menu {
  border: none;
  background: #f5f5f5;
}

.layout-main {
  background: #f0f2f5;
  padding: 20px;
}
</style>
